﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Twi.COMMON.Core;
using Twi.Modules.BIM;
using System.Data;
using System.Xml.Linq;

namespace Twi.Pages.BIM
{
    public class Authority
    {

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="userName"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public TwiReturn Login(string userName, string pwd)
        {
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                pwd = Twi.COMMON.Core.EncryptionAlgorithm.GetMD5(pwd);
                TwiUserInfo userInfo = _Authority.Instance.VerifyUserLogin(userName, pwd);
                UserContext.CurrentUser = userInfo;
                if (UserContext.CurrentUser != null)
                {
                    UserContext.AuthorityList = _Authority.Instance.GetAuthorityListByUserID(userInfo.FID);
                    twi.list = UserContext.AuthorityList;
                }
                twi.data = userInfo;
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="TableName"></param>
        /// <param name="oldpwd"></param>
        /// <param name="newpwd"></param>
        /// <returns></returns>
        public TwiReturn EditPwd(string TableName, string oldpwd, string newpwd)
        {
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            string userid = UserContext.CurrentUser.FID;
            try
            {
                UserContext.CheckRight(ref twi);
                oldpwd = EncryptionAlgorithm.GetMD5(oldpwd);
                newpwd = EncryptionAlgorithm.GetMD5(newpwd);
                string operatTag = _Authority.Instance.EditPwd(userid, TableName, oldpwd, newpwd);
                twi.success = true;
                twi.data = operatTag;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

        //注销
        public TwiReturn Logout()
        {
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                twi.success = true;
                UserContext.Session.Clear();
                UserContext.Session.Abandon();
                UserContext.AuthorityList = null;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }

            return twi;
        }

        /// <summary>
        /// 获取功能模块树
        /// </summary>
        /// <returns></returns>
        public string GetModuleTreeData()
        {
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                twi.data = _Authority.Instance.GetModuleTreeData();
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            string str = JSSerialize.Serialize(twi);
            str = str.Replace("\"dict\":", "\"attributes\":");
            return str;
        }

        /// <summary>
        /// 获取操作列表
        /// </summary>
        /// <param name="moduleName">功能模块名称</param>
        /// <param name="roleId">角色ID</param>
        /// <returns></returns>
        public DataTable GetOperationList(string moduleName, string roleID)
        {
            return _Authority.Instance.GetOperationList(moduleName, roleID);
        }

        /// <summary>
        /// 设置权限
        /// </summary>
        /// <param name="roleID"></param>
        /// <param name="operationID"></param>
        public TwiReturn SetRight(string roleID, string operationIDs)
        {
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                UserContext.CheckRight(ref twi);
                _Authority.Instance.SetRight(roleID, operationIDs.Split(','));
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

        /// <summary>
        /// 取消权限
        /// </summary>
        /// <param name="roleID"></param>
        /// <param name="operationID"></param>
        public TwiReturn CancelRight(string roleID, string operationIDs)
        {
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                UserContext.CheckRight(ref twi);
                _Authority.Instance.CancelRight(roleID, operationIDs.Split(','));
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

        /// <summary>
        /// 获取指定用户的功能菜单列表
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public TwiReturn GetCurrentUserMenuInfoList()
        {
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                UserContext.CheckRight(ref twi);
                twi.list = _Authority.Instance.GetMenuInfoListByUserID(UserContext.CurrentUser.FID);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }

        /// <summary>
        /// 获取指定用户的功能菜单树
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public TwiReturn GetMenuTreeByUserID(string userID)
        {
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                UserContext.CheckRight(ref twi);
                twi.data = _Authority.Instance.GetMenuTreeByUserID(UserContext.CurrentUser.FID);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }
        public TwiReturn TickteAdd()
        {
            TwiReturn twi = new TwiReturn();
            twi.hasRight = true;
            try
            {
                //UserContext.CheckRight(ref twi);
                //XElement xe = new XElement("SYS_PARAM", new XElement("ID", 100)
                //    , new XElement("PARAMCODE", "1000"), new XElement("PARAMNAME", "测试1")
                //    , new XElement("CREATE_TIME", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                XElement xe = new XElement("STO_INVALIDTICKET", new XElement("ID", 20)
                , new XElement("OPERATORCODE", "ADMIN")
                    , new XElement("CREATE_TIME", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
                    , new XElement("REGDATE", DateTime.Now.ToString("yyyy-MM-dd")));

                //XElement xe = new XElement("TKT_TRADEMAIN", new XElement("TRADEID", 1000), new XElement("TRADEDATE", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                twi.data = _Authority.Instance.TicketAdd(xe);
                twi.success = true;
            }
            catch (System.Exception ex)
            {
                twi.errors = ex.Message;
            }
            return twi;
        }
    }
}
